package com.songcha.module_main.ui.fragment.qmy

import android.app.Application
import androidx.lifecycle.MutableLiveData
import com.songcha.library_base.interfaces.IApiResult
import com.songcha.library_base.mvvm.base.BaseViewModel
import com.songcha.library_common.util.DateUtil
import com.songcha.library_common.util.LogUtil
import com.songcha.library_common.util.TimeUtil
import com.songcha.library_network.bean.BaseBean
import com.songcha.module_main.bean.calendar_task.CalendarTaskRecordBean
import com.songcha.module_main.event.RefreshGongDeEvent
import com.songcha.module_memo.bean.wooden_fish.AddGongDeBean
import com.songcha.module_memo.bean.wooden_fish.WoodenFishRecordBean
import org.greenrobot.eventbus.EventBus

class QmyViewModel(app:Application):BaseViewModel<QmyRepository>(app) {
    val recordBean = MutableLiveData<WoodenFishRecordBean.DataBean>(null)
    var realMaxStrikeCount = 0
    val medalList = MutableLiveData<List<AddGongDeBean.DataBean>>(listOf())
    private var hasTodayRecord = false

    fun getWoodenFishRecord(){
        handleApiDataObserver(repository.getWoodenFishRecord(),object :
            IApiResult<WoodenFishRecordBean> {
            override fun onSuccess(bean: WoodenFishRecordBean) {
                recordBean.value = bean.data
            }

        })
    }

    fun addGongDe(gongde:Int){
        handleApiDataObserver(repository.addGongDe(gongde),object : IApiResult<AddGongDeBean> {
            override fun onSuccess(bean: AddGongDeBean) {
                medalList.value = bean.data
                getWoodenFishRecord()
                EventBus.getDefault().post(RefreshGongDeEvent())
            }

        })

        //if(!hasTodayRecord)
            //updateCalendarTaskRecord()
    }

    fun addFakeGongDe(gongde:Int,continuousStrikeCount:Int){
        if(recordBean.value != null){
            var maxStrikeCount = recordBean.value!!.highestHit
            realMaxStrikeCount = maxStrikeCount
            if(continuousStrikeCount > maxStrikeCount)
                maxStrikeCount = continuousStrikeCount
            val tmp: WoodenFishRecordBean.DataBean? = WoodenFishRecordBean.DataBean(
                recordBean.value!!.todayMeritsVirtues + gongde,
                recordBean.value!!.totalMeritsVirtues + gongde,
                maxStrikeCount,
                recordBean.value!!.ranking,
            )
            recordBean.value = tmp!!
        }else{
            val tmp = WoodenFishRecordBean.DataBean(gongde,
                gongde,
                continuousStrikeCount,
                0,
            )
            recordBean.value = tmp
        }


    }

    fun setMaxStrike(count:Int) {
        if(recordBean != null){
            val tmp = WoodenFishRecordBean.DataBean(recordBean.value!!.todayMeritsVirtues ,
                recordBean.value!!.totalMeritsVirtues ,
                count,
                recordBean.value!!.ranking,
            )
            recordBean.value = tmp
        }
        handleApiDataObserver(repository.setMaxStrike(count),object : IApiResult<BaseBean> {
            override fun onSuccess(bean: BaseBean) {
                getWoodenFishRecord()
            }

        })
    }


    private fun currentDate2TimeStr():String{
        //val time = DateUtil.yearMonthDay2Calendar(currentDate.value!![0],currentDate.value!![1],currentDate.value!![2]).timeInMillis
        val str = TimeUtil.formatTime(TimeUtil.getTodayZeroTimeStamp() * 1000,"yyyy-MM-dd HH:mm")
        return str
    }

    private fun updateCalendarTaskRecord(){
        val bean = CalendarTaskRecordBean.DataBean()
        bean.createDate = currentDate2TimeStr()
        handleApiDataObserver(repository.updateCalendarTaskRecord(bean),object:IApiResult<BaseBean>{
            override fun onSuccess(bean: BaseBean) {
                //getCalendarTaskRecord()
                hasTodayRecord = true
            }

        })
    }

    fun getCalendarTaskRecord(){
        handleApiDataObserver(repository.getCalendarTaskRecord(currentDate2TimeStr()),object:IApiResult<CalendarTaskRecordBean>{
            override fun onSuccess(bean: CalendarTaskRecordBean) {
                if(bean.data != null){
                    hasTodayRecord = true
                }else{
                    hasTodayRecord = false
                }
            }

            override fun onError(e: Throwable) {

            }

        })
    }
}